package tomsung.cloud.agdc.controller;

import java.lang.reflect.Method;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;

import tomsung.cloud.agdc.common.Const;
import tomsung.cloud.agdc.model.JsonObject;
import tomsung.cloud.agdc.model.JsonRequest;
import tomsung.cloud.agdc.model.JsonResponse;

public class CommonController {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	protected JsonObject transer(final HttpServletRequest request, @RequestBody JsonRequest req) {
		JsonResponse rep = null;
		// 安全检查待添加
		String message = req.K;
		Method method;
		try {
			method = getClass().getDeclaredMethod(message, new Class[] { JsonRequest.class });
			rep = (JsonResponse) method.invoke(this, req);
		} catch (Exception e) {
			rep = req.getRep();
			rep.R = false;
			rep.ERR_MSG = "There is no message worker.";
			logger.error("There is no message {} worker.", message, e);
		}

		return rep;
	}
	
	protected long getSysID(JsonRequest req) {
		Object o = req.V.get(Const.SYSTEMID);
		if (o instanceof Integer) {
			return (Integer) o;
		} else if(o instanceof Long){
			return (Long) o;
		}
		else
		{
			return Long.valueOf(String.valueOf(o));
		}
	}
}
